package com.example.proxydemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @ProjectName: ProxyDemo
 * @Package: com.example.proxydemo
 * @ClassName: GrayFrameLayout
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2021/8/25 10:59
 * @UpdateUser: 更新者：
 * @UpdateDate: 2021/8/25 10:59
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class GrayFrameLayout extends FrameLayout {
    private Paint mPaint = new Paint();

    public GrayFrameLayout(@NonNull Context context) {
        this(context,null);
    }

    public GrayFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GrayFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int i = canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
        super.onDraw(canvas);
        canvas.restoreToCount(i);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int i = canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);
        canvas.restoreToCount(i);
    }
}

